package com.example.paimengshop;

import com.example.paimengshop.client_socket.ActionControl_Send;
import com.example.paimengshop.dataclass.Commodity;
import com.example.paimengshop.dataclass.Order;
import com.example.paimengshop.mypanes.HomeWidget;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class homeController implements Initializable {

    @FXML
    private AnchorPane anchorPane;

    @FXML
    public HBox rootHBox;

    @FXML
    public VBox hBox0;
    public VBox hBox1;
    public VBox hBox2;
    @FXML
    private ImageView homeTitle;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {


        //设置组件基本
        homeTitle.setImage(new Image("file:PaiMengShop/PaiMengShopClient/src/main/resources/com/example/paimengshop/beautifyImage/welcome.png"));


        //排列主页商品
        rootHBox.setPrefHeight(getRow(Data.allCommodity.size())*200);
        for(int i=0;i<Data.allCommodity.size();i++){
            HomeWidget homeWidget=new HomeWidget(Data.allCommodity.get(i));
            if(i%3==0){
                hBox0.getChildren().add(homeWidget);
            }else if(i%3==1){
                hBox1.getChildren().add(homeWidget);
            }else {
                hBox2.getChildren().add(homeWidget);
            }

            //设置鼠标单击事件
            int finalI = i;
            homeWidget.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent mouseEvent) {

                    //加载商品详情页
                    anchorPane.getChildren().clear();
                    anchorPane.getChildren().add(getCommodityDetail(Data.allCommodity.get(finalI)));

                }
            });
        }




    }

    public static int getRow(int number){

        int row=0;
        if(number%3==0){
            row=number/3;
        }else {
            row=number/3+1;
        }
        return row;
    }

    //获取商品详情pane
    public static AnchorPane getCommodityDetail(Commodity commodity){
        AnchorPane root=new AnchorPane();
        root.setPrefSize(1200, 700);

            VBox vBox1=new VBox();
            vBox1.setPrefSize(1200, 700);

                HBox hBox1=new HBox();
                hBox1.setPrefSize(1200,500);
                hBox1.setAlignment(Pos.CENTER);
                hBox1.setSpacing(50);

                    ImageView imageView=new ImageView();
                    imageView.setImage(new Image(commodity.getImageURL()));
                    imageView.setFitWidth(430);
                    imageView.setFitHeight(430);

                    VBox vBox2=new VBox();
                    vBox2.setPrefSize(600, 500);
                    vBox2.setSpacing(10);

                        Label commodityName=new Label(commodity.getName());
                        commodityName.setPrefSize(600, 180);
                        commodityName.setWrapText(true);
                        commodityName.setAlignment(Pos.CENTER_LEFT);
                        commodityName.setFont(new Font(60));

                        Label priceLabel=new Label(commodity.getPrice()+"元");
                        priceLabel.setPrefSize(600, 100);
                        priceLabel.setAlignment(Pos.CENTER_RIGHT);
                        priceLabel.setFont(new Font(45));

                        Label introduction=new Label(commodity.getIntroduction());
                        introduction.setPrefSize(600, 180);
                        introduction.setWrapText(true);
                        introduction.setFont(new Font(30));

                    vBox2.getChildren().addAll(commodityName, priceLabel, introduction);

                hBox1.getChildren().addAll(imageView, vBox2);

                HBox hBox2=new HBox();
                hBox2.setPrefSize(1200, 200);
                hBox2.setSpacing(100);
                hBox2.setAlignment(Pos.CENTER);

                    Button collectionButton=new Button("收藏");
                    collectionButton.setPrefSize(180, 100);

                    Button cartButton=new Button("购物车");
                    cartButton.setPrefSize(180, 100);

                    Button buyButton=new Button("购买");
                    buyButton.setPrefSize(180, 100);

                hBox2.getChildren().addAll(collectionButton, cartButton, buyButton);

            vBox1.getChildren().addAll(hBox1, hBox2);

        root.getChildren().add(vBox1);


        //鼠标事件处理
        //加收藏夹
        collectionButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                if(Data.currentUser.getCollection().contains(commodity))
                {
                    Alert alert =new Alert(Alert.AlertType.INFORMATION);
                    alert.setTitle("添加收藏");
                    alert.setContentText("您已收藏该商品！");
                    alert.setHeaderText("重复收藏");
                    alert.showAndWait();
                }
                else
                {
                    main.getSend().Send(ActionControl_Send.UpdateCollection(Data.getCurrentUser(), Data.getCurrentUser().getCollection()));
                    Data.currentUser.getCollection().add(commodity);
                }
            }
        });

        //加购物车
        cartButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Data.currentUser.getCart().add(commodity);
                main.getSend().Send(ActionControl_Send.UpdateCart(Data.getCurrentUser(), Data.getCurrentUser().getCart()));
            }
        });

        //直接购买，并跳到订单页
        buyButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                //创建一个只有一个商品的商品列表
                ArrayList<Commodity> good=new ArrayList<>();
                good.add(commodity);

                //创建新订单
                Order order=new Order(-1, good, Data.currentUser.getId(), commodity.getPrice(), Data.currentUser.getAddress(), 1);

                main.getSend().Send(ActionControl_Send.CreateOrder(Data.currentUser, order));
                //添加订单
                Data.orders.add(order);
            }
        });





        return root;
    }
}
